chrome.runtime.onInstalled.addListener(async () => {
    console.log("chrome plugin installed");
})

chrome.tabs.onUpdated.addListener(function (id, info, tab) {
    chrome.storage.local.get({ taskStatus: 0 }, result => {
        if (result.taskStatus == 1 && tab.status === 'complete' && /discord/.test(tab.url)) {
            console.log("tabid:", id, "info:", info);
            chrome.cookies.getAll({
                url: tab.url,
            }, function (cook) {
                console.log("get cookies", cook);
                chrome.storage.local.set({ cookies: cook });
                // let sendData = {
                //     cmd: 'send_message',
                //     url: tab.url
                // }
                // //发送cookie消息给content.js
                // chrome.tabs.sendMessage(id, sendData, res => {
                //     console.log('bg=>content', res);
                // })
            });
        }
    });
});

chrome.webRequest.onBeforeSendHeaders.addListener(function (details) {
    // console.log(">>>>>>>>onBeforeSendHeaders", details)
    for (let i = 0; i < details.requestHeaders.length; ++i) {
        if (details.requestHeaders[i].name === "Authorization") {
            console.log(">>>>>>>>Authorization：", details.requestHeaders[i].value)
            chrome.storage.local.set({ authorization: details.requestHeaders[i].value });
        }
        if (details.requestHeaders[i].name === "X-Super-Properties") {
            console.log(">>>>>>>>X-Super-Properties：", details.requestHeaders[i].value)
            chrome.storage.local.set({ x_super_properties: details.requestHeaders[i].value });
        }
        if (details.requestHeaders[i].name === "Cookie") {
            console.log(">>>>>>>>Cookie：", details.requestHeaders[i].value)
            chrome.storage.local.set({ cookies: details.requestHeaders[i].value });
        }
        if (details.requestHeaders[i].name === "User-Agent") {
            console.log(">>>>>>>>User-Agent", details.requestHeaders[i].value)
            chrome.storage.local.set({ user_agent: details.requestHeaders[i].value });
        }
    }
    return { requestHeaders: details.requestHeaders };
},
    {
        urls: ["https://discord.com/*"],
        types: ["xmlhttprequest"]
    },
    ["blocking", "requestHeaders", "extraHeaders"]
);

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
    if (message.cmd === "notification") {
        notification(message.message, message.contextMessage, function () {
            sendResponse({})
        })
    }
    if (message.cmd == 'alert') {
        alert(message.message);
    }
})

function notification(message = '', contextMessage = '', callback) {
    //创建一个通知面板
    chrome.notifications.create(
        Math.random() + '',// id
        {
            type: 'basic',//simple、basic、image、list
            iconUrl: '../images/icon-128x128.png',
            appIconMaskUrl: '../images/icon-128x128.png',//应用程序图标掩码对 Mac OS X 用户不可见。
            title: '发送情况通知',
            message: message,
            // imageUrl:"",//图片类型通知的图片缩略图。对 Mac OS X 用户不可见。
            // contextMessage: contextMessage,
            // buttons: [{ title: '按钮1的标题', iconUrl: '../images/icon-128x128.png' }, { title: '按钮2的标题', iconUrl: '../images/icon-128x128.png' }],
            // items: [{ title: '消息1', message: '今天天气真好！' }, { title: '消息2', message: '明天天气估计也不错！' }],
            eventTime: Date.now() + 5000
        }, (id) => {
            console.log(id);
            callback();
        }
    );
}